package com.net.xpay.admin;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.net.xpay.admin.interceptor.MvcAccessLogInterceptor;
import com.net.xpay.admin.secutiry.CurrentOperatorArgumentResolver;
import com.net.xpay.admin.secutiry.RequestUserArgumentResolver;
import com.net.common.enums.spring.StringEnumConverterFactory;
import com.net.xpay.core.CoreProperties;
import com.net.xpay.core.helper.EnvironmentHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;
import java.nio.charset.Charset;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
  on 28/01/2018.
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private CoreProperties coreProperties;

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; //DateTime序列化的格式

    @Autowired
    private EnvironmentHelper environmentHelper;

    //线上的redis 不配置这个会报错 意思是关闭redis的GET CONFIG操作
    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }

    /**
     * 文件上传临时路径
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        if (environmentHelper.isProd()) {
            factory.setLocation(coreProperties.getTomcatUploadDir());
        }
        return factory.createMultipartConfig();
    }

    @Bean
    public MvcAccessLogInterceptor mvcAccessLogInterceptor() {
        return new MvcAccessLogInterceptor();
    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        stringHttpMessageConverter.setWriteAcceptCharset(false);
        converters.add(stringHttpMessageConverter);
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(mvcAccessLogInterceptor());
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setDateFormatter(DateTimeFormatter.ISO_DATE);
        registrar.setTimeFormatter(DateTimeFormatter.ISO_TIME);
        registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
        registrar.registerFormatters(registry);

        registry.addConverterFactory(new StringEnumConverterFactory());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new CurrentOperatorArgumentResolver());
        argumentResolvers.add(new RequestUserArgumentResolver());
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
